package com.xafc.demo.sys.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xafc.demo.sys.model.dto.MenuDTreeDto;
import com.xafc.demo.sys.model.dto.MenuDto;
import com.xafc.demo.sys.model.dto.MenuTreeDto;
import com.xafc.demo.sys.model.entity.Menu;
import com.xafc.demo.framework.result.ApiResult;
import com.xafc.demo.framework.result.PageResult;
import com.xafc.demo.framework.result.ResultCode;
import com.xafc.demo.sys.model.query.MenuPageQuery;
import com.xafc.demo.sys.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="权限相关接口")
@RestController
@RequestMapping("/api/sys/menu")
public class MenuApi {


    @Autowired
    private IMenuService menuService;

    @ApiOperation(value = "根据分页获取权限列表")
    @PreAuthorize("hasAuthority('API_SYS_MENU_LIST')")
    @GetMapping("/list")
    public PageResult<MenuDto> list(MenuPageQuery query){
        Page<MenuDto> page = this.menuService.getMenuList(query);
        return PageResult.success(page);
    }

    /**
     * 标准的方式返回数据
     * @return
     */
    @ApiOperation(value = "树形结果返回权限列表")
    @GetMapping("/tree")
    public ApiResult<List<MenuTreeDto>> tree(){
        List<MenuTreeDto> menuTreeDtoList = this.menuService.getMenuTree();
        return ApiResult.success(menuTreeDtoList);
    }

    /**
     * 菜单渲染时使用
     * @return
     */
    @GetMapping("/mtree")
    @ApiOperation(value = "树形结果返回权限列表 - mtree菜单格式")
    public List<MenuTreeDto> mtree(){
        return this.menuService.getMenuTree();
    }

    /**
     * dtree渲染时使用
     * @return
     */
    @ApiOperation(value = "树形结果返回权限列表 - dtree菜单格式")
    @GetMapping("/dtree")
    public ApiResult<List<MenuDTreeDto>> dtree(){
        List<MenuDTreeDto> menuTreeDtoList = this.menuService.getMenuDTree();
        return ApiResult.success(0, menuTreeDtoList);
    }

    @ApiOperation(value = "根据ID获取权限信息")
    @PreAuthorize("hasAuthority('API_SYS_MENU_UPDATE')")
    @GetMapping("/get/{id}")
    public ApiResult<Menu> detail(@PathVariable("id") Integer id){
        Menu dept = menuService.getMenuById(id);
        return ApiResult.success(dept);
    }

    @ApiOperation(value = "新增权限")
    @PreAuthorize("hasAuthority('API_SYS_MENU_ADD')")
    @GetMapping("/add")
    public ApiResult add(Menu menu){
        ResultCode resultCode = menuService.addMenu(menu);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "更新权限")
    @PreAuthorize("hasAuthority('API_SYS_MENU_UPDATE')")
    @PostMapping("/update")
    public ApiResult edit(Menu menu){
        ResultCode resultCode = menuService.updateMenu(menu);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "删除权限")
    @PreAuthorize("hasAuthority('API_SYS_MENU_DELETE')")
    @PostMapping("/delete")
    public ApiResult delete(@RequestParam("id") Integer id){
        Integer count = menuService.getCountByPid(id);
        if (count > 0){
            return ApiResult.fail("存在子菜单，不能删除");
        }

        // 查询是否存在子菜单
        ResultCode resultCode = menuService.deleteMenu(id);
        return ApiResult.value(resultCode);
    }
}

